package com.i2works.smartluolong.web.service.merchant.impl;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.base.FlowItemTypeEnum;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.base.MerchantStatusEnum;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.utils.redis.RedisSessionDAO;
import com.i2works.smartluolong.web.dal.merchant.dao.IMerchantApplyMemberDAO;
import com.i2works.smartluolong.web.entity.common.HoliDayQuery;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.items.ItemsDO;
import com.i2works.smartluolong.web.entity.items.ItemsQuery;
import com.i2works.smartluolong.web.entity.merchant.MerchantApplyDO;
import com.i2works.smartluolong.web.entity.merchant.MerchantApplyMemberDO;
import com.i2works.smartluolong.web.entity.merchant.MerchantApplyMemberQuery;
import com.i2works.smartluolong.web.service.common.IHolidayService;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.items.IItemsService;
import com.i2works.smartluolong.web.service.merchant.IMerchantApplyMemberService;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 个体商户申请 - 商户个人信息业务层接口
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/7/23 21:10
 */
@Service("merchantApplyMemberService")
public class MerchantApplyMemberServiceImpl extends BaseCommonServiceImpl<MerchantApplyMemberQuery, MerchantApplyMemberDO> implements IMerchantApplyMemberService {

    private final IMerchantApplyMemberDAO merchantApplyMemberDAO;
    private final IFlowInfoService flowInfoService;
    private final IHolidayService holidayService;
    private final RedisSessionDAO redisSessionDAO;
    private final IItemsService itemsService;

    @Autowired
    public MerchantApplyMemberServiceImpl(IMerchantApplyMemberDAO merchantApplyMemberDAO, IFlowInfoService flowInfoService, IHolidayService holidayService, RedisSessionDAO redisSessionDAO, IItemsService itemsService) {
        this.merchantApplyMemberDAO = merchantApplyMemberDAO;
        this.flowInfoService = flowInfoService;
        this.holidayService = holidayService;
        this.redisSessionDAO = redisSessionDAO;
        this.itemsService = itemsService;
    }

    /**
     * 保存商户信息
     */
    @Override
    public void saveMember(MerchantApplyMemberDO merchantApplyMember, MerchantApplyDO merchantApply) throws ServiceException {
        try {
            if (merchantApplyMember.getId() != null) {
                merchantApplyMemberDAO.update(merchantApplyMember);
            } else {
                merchantApplyMemberDAO.save(merchantApplyMember);
            }
            //添加工作流
            FlowInfoDO flowInfoDO = new FlowInfoDO();
            flowInfoDO.setItemId(merchantApply.getId());
            flowInfoDO.setComplete(0);
            flowInfoDO.setItemName(merchantApply.getMerchantName() + "-" + FlowItemTypeEnum.MERCHANT_APPLY.getValue());
            flowInfoDO.setItemType(FlowItemTypeEnum.MERCHANT_APPLY.getKey());
//            flowInfoDO.setItemStatus(MerchantStatusEnum.STATUS_7.getKey());
            //fixme 如果添加自拍和签名，需要设置status = 7 如上段代码
            flowInfoDO.setItemStatus(MerchantStatusEnum.STATUS_1.getKey());
            flowInfoDO.setUserId(merchantApply.getUserId());
            flowInfoDO.setUpdateName(merchantApplyMember.getUpdateName());
//            flowInfoDO.setMessage("用户《" + flowInfoDO.getUpdateName() + "》提交资料，待补充自拍照和个人签名。");
            //fixme 如果添加自拍和签名，需要设置status = 7 如上段代码
            flowInfoDO.setMessage("用户《" + flowInfoDO.getUpdateName() + "》提交审核");
            //添加事项期限的级别和时间
            flowInfoDO.setTermLevel(GlobalConstant.termLevel.L1);
            flowInfoDO.setDeadline(getDeadline(FlowItemTypeEnum.MERCHANT_APPLY.getKey()));
            flowInfoService.save(flowInfoDO);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 获取事项到期日期
     */
    private Date getDeadline(String itemType) throws ServiceException {
        List<String> dateList = Lists.newArrayList();
        //查询节假日列表
        String dateListTags = GlobalConstant.redis.DATE_LIST_TAGS;
        List list = redisSessionDAO.get(dateListTags, List.class);
        if (CollUtil.isNotEmpty(list)) {
            dateList.addAll(list);
        } else {
            List<String> allDateList = holidayService.queryAll(new HoliDayQuery());
            redisSessionDAO.set(dateListTags, allDateList);
            dateList.addAll(allDateList);
        }
        //查询天数
        ItemsQuery itemsQuery = new ItemsQuery();
        itemsQuery.setItemType(itemType);
        ItemsDO itemsDO = itemsService.query(itemsQuery);
        Date workday;
        if (itemsDO != null) {
            workday = BaseUtils.workday(dateList, new Date(), itemsDO.getPromiseTerm(), false);
        } else {
            workday = BaseUtils.workday(dateList, new Date(), 5, false);
        }
        return workday;
    }
}

